<?php

class OrdersModel extends Model{

		public $Model = array();

		public function _initialize(){
			// $this->Model['Orders'] = new OrdersModel();
        	$this->Model['Currency'] = new CurrencyModel();
		}

		/**
		 * 挂单
		 * @param int  $num 数量
		 * @param float $price 价格
		 * @param char $type 买buy 卖sell
		 * @param $currency_id 交易币种
		 */
		public function thust($member_id,$num,$price,$type,$currency){
			//获取交易币种信息
			switch ($type){
				case 'buy':
					$fee=$currency['currency_buy_fee'];
					break;
				case 'sell':
					$fee=$currency['currency_sell_fee'];
					break;
			}
			$data=array(
					'member_id'=>$member_id,
					'currency_id'=>$currency['currency_id'],
					'currency_trade_id'=>$currency['trade_currency_id'],
					'price'=>$price,
					'num'=>$num,
					'trade_num'=>0,
					'fee'=>$fee,
					'type'=>$type,
					'status'=>0,
					'add_time'=>time()
			);
			if ($rs=$this->add($data)){
					$msg=$rs;
			}else {
					$msg=0;
			}
			return $msg;
		}


		/**
		 *撤销订单
		 * @param   int $list  订货单信息
		 * @param   int $member_id  用户id
		 * @param   int $order_id  订单号 id
		 */
		public  function cancelOrdersByOrderId($one_order){
		    $this->startTrans();
		    $r[]=$this->getOneOrdersByMemberAndOrderId($one_order['member_id'],$one_order['orders_id'],array(0,1));
		    $r[]=$this->setOrdersStatusByOrdersId(-1, $one_order['orders_id']);
		    //返还资金
		    switch ($one_order['type']){
		        case 'buy':
		           	$money=($one_order['num']-$one_order['trade_num'])*$one_order['price'];
	        	   	$r[]= $this->Model['Currency']->setUserMoney($one_order['member_id'], $one_order['currency_trade_id'],$money , 'inc', 'num');
		           	$r[]= $this->Model['Currency']->setUserMoney($one_order['member_id'], $one_order['currency_trade_id'], $money, 'dec', 'forzen_num');
		           	break;
			    case 'sell':
			        $num=$one_order['num']-$one_order['trade_num'] ;
	    	        $r[]= $this->Model['Currency']->setUserMoney($one_order['member_id'], $one_order['currency_id'],$num, 'inc', 'num');
	    	        $r[]= $this->Model['Currency']->setUserMoney($one_order['member_id'], $one_order['currency_id'], $num, 'dec', 'forzen_num');
	    	        break;
		    }
		    //更新订单状态
		    if(!in_array(false, $r)){
		        $this->commit();
		        $info['status'] =1;
		        $info['info'] = '撤销成功';
		        return $info;
		    }else{
		        $this->rollback();
		        $info['status'] = -1;
		        $info['info'] = '撤销失败';
		        return $info;
		    }
		}

		//获取挂单记录
		public function getOrders($type,$currency_id,$trade_currency_id){
			switch ($type){
				case 'buy':  return $this->getOrdersByType($currency_id,$trade_currency_id,'buy', 30, 'desc');
				break;
				case 'sell': return $this->getOrdersByType($currency_id,$trade_currency_id,'sell', 30, 'asc');
				break;
			}
		}

		/**
		 * 返回用户第一条未成交的挂单
		 * @param int $memberId 用户id
		 * @param int $currencyId 币种id
		 * @return array 挂单记录
		 */
		public function getFirstOrdersByMember($memberId,$type,$currencyId,$trade_currency_id){
			$where['member_id']=$memberId;
			$where['currency_id']=$currencyId;
			$where['currency_trade_id']=$trade_currency_id;
			$where['type']=$type;
			$where['status']=array('in',array(0,1));
			return $this->where($where)->order('add_time desc')->find();
		}

		/**
		 * 返回一条挂单记录
		 * @param int $currencyId 币种id
		 * @param float $price 交易价格
		 * @return array 挂单记录
		 */
		public function getOneOrders($type,$currencyId,$trade_currency_id,$price){
			switch ($type){
					case 'buy':$gl='egt';$order='price desc,add_time asc'; break;
					case 'sell':$gl='elt'; $order='price asc,add_time asc';break;
			}
			$where['currency_id']=$currencyId;
			$where['currency_trade_id']=$trade_currency_id;
			$where['type']=$type;
			$where['price']=array($gl,$price);
			$where['status']=array('in',array(0,1));
			return $this->where($where)->order('add_time desc')->order($order)->find();
		}



		/**
		* 返回指定数量排序的挂单记录
		* @param char $type buy sell
		* @param int $num 数量
		* @param char $order 排序 desc asc
		*/
		public function getOrdersByType($currencyid,$trade_currency_id,$type,$num,$order){
			$where['type']=array('eq',$type);
			$where['status']=array('in',array(0,1));
			$where['currency_id']=$currencyid;
			$where['currency_trade_id']=$trade_currency_id;
			$list= $this->field("sum(num) as num,sum(trade_num) as trade_num,price,type,status")->where($where)->group('price')->order("price $order, add_time asc,orders_id desc")->limit($num)->select();
			return $list;
		}


		/**
		* 获取一个挂单记录价格 买一 卖一
		* @param unknown $currencyid
		* @param unknown $type
		* @param unknown $order
		*/
		public function getOneOrdersByPrice($currencyid,$trade_currency_id,$type){
			$where['currency_id']=$currencyid;
			$where['currency_trade_id']=$trade_currency_id;
			$where['type']=$type;
			$where['status']=array('in',array(0,1));
			switch ($type){
					case 'buy': $order='desc';
					break;
					case 'sell':$order='asc';
					break;
			}
			$orders= $this->field('price')->where($where)->order('price '.$order)->find();
			return $orders['price']?$orders['price']:0;
		}

		/**
		 * 返回指定用户挂单记录
		 * @param int $member_id
		 * @param int $order_id
		 * @param array $status
		 */
		public function getOneOrdersByMemberAndOrderId($member_id,$order_id,$status=array(0,1,2,-1)){
		    $where['member_id']=$member_id;
		    $where['orders_id']=$order_id;
		    $where['status']=array('in',$status);
		    $one_order=$this->where($where)->find();
		    return  $one_order;
		}


		/**
		 * 返回指定用户挂单记录
		 * @param int $member_id
		 * @param int $order_id
		 * @param array $status
		 */
		public function getOrdersByMember($member_id,$currency_id,$trade_currency_id,$num){
		    $where['member_id']=$member_id;
		    $where['currency_id']=$currency_id;
		    $where['currency_trade_id']=$trade_currency_id;
		    $where['status']=array('in',array(0,1));
		    $orders=$this->where($where)->limit($num)->order("add_time desc")->select();
		    foreach ($orders as $key => $value) {
		    	$orders[$key]['addtime'] = date('Y-m-d H:i:s',$value['add_time']);
		    }
		    return  $orders;
		}



		/**
		 * 设置订单状态
		 * @param int $status  0 1 2 -1
		 * @param int $orders_id 订单id
		 * @return  boolean  
		 */
		public function setOrdersStatusByOrdersId($status,$orders_id){
		    return $this->where("orders_id=$orders_id")->setField('status',$status);
		}

}